package com.gxd.dao.impl;

import com.gxd.dao.GoodTypeDao;
import com.gxd.domain.GoodsType;
import com.gxd.util.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodTypeDao {
    QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
    @Override
    public List<GoodsType> findByLevel(int i) {
        String sql=" select id,name,level,Parent from tb_goods_type where  level=? ";
        List<GoodsType> goodsTypes = null;
        try {
            goodsTypes = queryRunner.query(sql, new BeanListHandler<>(GoodsType.class), i);
            return goodsTypes;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public GoodsType findById(int typeid) {
        String sql=" select id,name,level,Parent from tb_goods_type where id=? ";
        try {
            return queryRunner.query(sql,new BeanHandler<>(GoodsType.class),typeid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
